package dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import pojo.Book;
import util.DBUtil;
import dao.BookDao;

public class BookDaoImpl implements BookDao {

	public int addBook(Book b) {
		String sql = "insert into book(id, name, publishDate, author, price, isNew, PUBLISHER) values(seq.nextval, ?, ?, ?, ?, ?, ?)";//先创建一个模板
		PreparedStatement pst = null;
		Connection con = null;
		int m = 0;
		
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			
			pst.setString(1, b.getName());
			pst.setDate(2, new java.sql.Date(b.getPublishDate().getTime()));
			pst.setString(3, b.getAuthor());
			pst.setDouble(4, b.getPrice());
			pst.setBoolean(5, b.isNew());
			pst.setString(6, b.getPublisher());
			
			m = pst.executeUpdate();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	public int deleteBook(Book b) {
		String sql = "delete book where id=?";//先创建一个模板
		PreparedStatement pst = null;
		Connection con = null;
		int m = 0;
		
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			
			pst.setInt(1, b.getId());
			m = pst.executeUpdate();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	public int updateBook(Book b) {
		String sql = "update book  set name=? where id=?";//先创建一个模板
		PreparedStatement pst = null;
		Connection con = null;
		int m = 0;
		
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			
			pst.setString(1, b.getName());
			pst.setInt(2, b.getId());
			m = pst.executeUpdate();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	public Book loadBook(Integer id) {
		Book book = null;
		String sql = "select * from book where id=?";//先创建一个模板
		PreparedStatement pst = null;
		Connection con = null;
		int m = 0;
		
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			
			pst.setInt(1, id);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				book = new Book();
				book.setId(rs.getInt("id"));
				book.setName(rs.getString("name"));
				book.setPublishDate(new Date(rs.getDate("publishDate").getTime()));
				book.setAuthor(rs.getString("author"));
				book.setPrice(rs.getDouble("price"));
				book.setNew(rs.getBoolean("isNew"));
				book.setPublisher(rs.getString("publisher"));
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, null);
		}
		
		return book;
	}

	public List<Book> findAll() {
		List<Book> list = new ArrayList<Book>();
		Book book = null;
		String sql = "select * from book";//先创建一个模板
		PreparedStatement pst = null;
		Connection con = null;
		int m = 0;
		
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				book = new Book();
				book.setId(rs.getInt("id"));
				book.setName(rs.getString("name"));
				book.setPublishDate(new Date(rs.getDate("publishDate").getTime()));
				book.setAuthor(rs.getString("author"));
				book.setPrice(rs.getDouble("price"));
				book.setNew(rs.getBoolean("isNew"));
				book.setPublisher(rs.getString("publisher"));
				
				list.add(book);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, null);
		}
		
		return list;
	}

}
